package com.android2024.no6_music;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;

import java.io.IOException;

/** 启动服务 在服务中进行音乐的播放 **/
public class MusicService extends Service {
    //多媒体类，用于音乐的播放
    private MediaPlayer mPlayer = new MediaPlayer();
    //服务器接口类
    class MusicBinder extends Binder implements MediaPlayer.OnPreparedListener {
        MusicData.Music music = null;
        //播放音乐
        public void play(MusicData.Music music){
            boolean loadMusic = false;
            stop();
            if(this.music==null||this.music!=music) {
                //如果需要播放新的音乐 加载音乐资源
                if(mPlayer!=null)
                mPlayer.reset();

                if(music.type == 0){
                    mPlayer = MediaPlayer.create(MusicService.this,music.rawid);
                }else if(music.type==1) {
                    try {
                        mPlayer.setDataSource(music.url);
                        mPlayer.prepare();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }else if(music.type==2){
                    try {
                        Log.d("log","### url:"+MusicData.httpurl+music.url);
                        Message msg = new Message();
                        msg.what = 2;
                        MainActivity.handler.sendMessage(msg);
                        mPlayer.setDataSource(MusicData.httpurl+music.url);
                        //mPlayer.prepare();
                        mPlayer.prepareAsync();
                        loadMusic = true;
                        mPlayer.setOnPreparedListener(this);

                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            if(mPlayer!=null&&!loadMusic) {
                mPlayer.start();
                start();
            }
            this.music = music;

        }

        //音乐暂停
        public void pause(){
            mPlayer.pause();
            stop();
        }

        //进度条拖到音乐播放
        public void seekTo(int time){
            mPlayer.seekTo(time);
        }

        @Override
        public void onPrepared(MediaPlayer mediaPlayer) {
            Log.d("log","### @@  onPrepared :");
            mPlayer.start();
            start();
            Message msg = new Message();
            msg.what = 3;
            MainActivity.handler.sendMessage(msg);
        }
    }

    public MusicService() {
    }

    //服务结束时 是否资源
    @Override
    public void onDestroy() {
        super.onDestroy();
        if(mPlayer!=null)
        {
            //停止播放，释放资源
            if(mPlayer.isPlaying())
                mPlayer.stop();
            mPlayer.release();
            mPlayer=null;
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        //throw new UnsupportedOperationException("Not yet implemented");
        //服务器绑定成功 返回 服务器的数据接口
        return new MusicBinder();
    }

    //音乐时间类
    class MusicTime{
        //总时长，当前播放的时间
        public int maxTime,time;
    }
    /** 定时器功能 **/
    private Handler handler = new Handler();
    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            // handler自带方法实现定时器
            try {
                if(mPlayer!=null) {
                    MusicTime musicTime = new MusicTime();
                    //总时长
                    musicTime.maxTime = mPlayer.getDuration();
                    //当前播放的时间
                    musicTime.time = mPlayer.getCurrentPosition();
                    if(!mPlayer.isPlaying()) musicTime.time = musicTime.maxTime;
                    Message message = new Message();
                    message.what = 0;
                    message.obj = musicTime;
                    MainActivity.handler.sendMessage(message);
                    start();
                }
            } catch (Exception e) {
                // TODO Auto-generated catch block
            }
        }
    };
    //启动定时器
    private void start(){
        stop();
        handler.postDelayed(runnable,100);
    }
    //结束定时器
    private void stop(){
        handler.removeCallbacks(runnable);
    }
}